<?php

require_once 'web.php';

/**
 * Description of UserController
 *
 * @author user
 */
class UserController extends WebController {

    public function login() {
        if ($this->auth_user->is_login()) {
            URL::redirect('photo');
        }

        if (isset($_POST['username'])) {
            if ($this->auth_user->login($_POST['username'], $_POST['password'])) {
                URL::redirect('photo');
            }
        }

        URL::redirect('home');
    }

    public function logout() {
        $this->auth_user->logout();
        URL::redirect('photo');
    }

    public function follow($id) {
        // Init add follow
        $auth_user = $this->auth_user->user;
        $followings = new ORM('followings');
        $followings->follower = $auth_user->id;
        $followings->followee = $id;

        // Save follow
        if($followings->save()) {            
            // Init add notif
            $notif = new ORM('notif');
            $notif->receiver_id = $id;
            $notif->content = "<a href=\"" . URL::site("profile?uid=$auth_user->id") . "\">$auth_user->username</a> followed you.";
            $notif->time_stamp = time();

            // Save notif
            if ($notif->save()) {
                URL::redirect("photo?uid=$id");
            } else {
                show_error('Cannot add notid follow user with id ' . $id);
            }
            
        } else {
            show_error('Cannot follow user with id ' . $id);
        }
    }

    public function unfollow($id) {
        // Init unfollow
        $followings = new ORM('followings');
        $followings->where("`follower` = '" . $this->auth_user->user->id . "' AND `followee` = '$id'")->find();

        // Unfollow
        if($followings->delete()) {
            URL::redirect("photo?uid=$id");
        } else {
            show_error('Cannot unfollow user with id ' . $id);
        }
    }

    public function block($user_id) {
        $auth_user = $this->auth_user->user;
        if ($auth_user->role != "admin") {
            URL::redirect("profile");
        }

        $user = new ORM('user');
        $user->find($user_id);
        if($user->id != null) {
            $user->role = "blocked";
            if(!$user->save()) {
                show_error("Cannot block user id $user_id");
            }
        } else {
            URL::redirect("profile");
        }

        URL::redirect("profile?uid=$user_id");
    }

    public function unblock($user_id) {
        $auth_user = $this->auth_user->user;
        if ($auth_user->role != "admin") {
            URL::redirect("profile");
        }

        $user = new ORM('user');
        $user->find($user_id);
        if($user->id != null) {
            $user->role = "user";
            if(!$user->save()) {
                show_error("Cannot unblock user id $user_id");
            }
        } else {
            URL::redirect("profile");
        }

        URL::redirect("profile?uid=$user_id");
    }
}

?>
